package pers.archives.loginToken.login;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;
import pers.archives.common.entity.SecurityUser;
import pers.archives.common.entity.User;
import pers.archives.common.ov.OVUser;
import pers.archives.loginToken.service.UserService;

/*
 *@program:archives-parent
 *@author: 赵浩浩
 *@Time: 2020/9/12  12:55
 */
@Slf4j
@Component("userDetailsService")
public class  ArchivesUserDetailsServices implements UserDetailsService,SocialUserDetailsService {


    @Autowired
    private UserService userService;

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            log.error("非法用户名登录");
            throw new UsernameNotFoundException("非法用户名登录");
        }
        User user = userService.findByUserName(username);
        if (user == null) {
            log.error("用户不存在 -> [{}]", username);
            throw new UsernameNotFoundException("用户不存在");
        }
        return new SecurityUser(user);
    }





}
